//
//  getProductListViewController.m
//  BAOASystem
//
//  Created by michael on 2018/2/26.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "getProductListTableViewCell.h"
#import "addProductListViewController.h"
#import "getProductListViewController.h"

@interface getProductListViewController ()

@end

@implementation getProductListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName, nil];
 
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0a62ea"];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    self.title = NSLocalizedString(@"商品列表", nil);
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
      self.navigationController.navigationBar.hidden = NO;
    // Do any additional setup after loading the view.
      _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,KScreenHeight-kTopHeight) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

 __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{
        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
//        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
   if (weakSelf.lastPage==0 ){

//        if (weakSelf.isSearching) {
////     [self loadSearchMore];
//}
//     else {
     [weakSelf loadMore];
//     }
        }else{
            
            [weakSelf.tableView footerEndRefreshing];
            [weakSelf.view makeToast:NSLocalizedString(@"没有更多了", nil)];
            
        }
        
    }];
     UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"]style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
 self.navigationItem.leftBarButtonItem = item1;
}
- (void)buttonClick{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)loadData{

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><username>%@</username><title></title><pdstate></pdstate><pageNumber>%d</pageNumber><pageSize>20</pageSize></body></message>",userName,password,website,language,BAtype,userName,_currentPage];
//NSString *bodyStr = @"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>chgy1234</userName><password>8c13b5750412d922b01b2da95d24f8b6</password><website>1</website><language>0</language><type>1</type></header><body><username>chgy1234</username><title></title><pdstate></pdstate><pageNumber>1</pageNumber><pageSize>20</pageSize></body></message>";
  bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bgetProductList];
  [HttpTools requestWithURL:path arrays:array method:@"getProductList" success:^(id JSON) {
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  NSLog(@"%@",JSON);
   if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
                  if([[NSString stringWithFormat:@"%@",JSON[@"code"]] isEqualToString:@"200"]){
                    
        _dataArray =   [NSMutableArray array];
NSDictionary *dic = JSON[@"ProductsList"];
NSArray *array =dic[@"list"];

for (NSDictionary *dic in array ) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}


_lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
[_tableView reloadData];
    [self.tableView headerEndRefreshing];

//        [self.tableView footerEndRefreshing];
        self.currentPage++;
                  }else{
                  
                     [self.view makeToast:JSON[@"msg"]];

                  }
    } failure:^(NSError *error) {
            [self.tableView headerEndRefreshing];

//        [self.tableView footerEndRefreshing];
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

    }];
}
- (void)loadMore{
// [MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
//NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><username>%@</username><title></title><pdstate></pdstate><pageNumber>%d</pageNumber><pageSize>20</pageSize></body></message>",userName,password,website,language,BAtype,userName,_currentPage];
NSString *bodyStr =  [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>chgy1234</userName><password>8c13b5750412d922b01b2da95d24f8b6</password><website>1</website><language>0</language><type>1</type></header><body><username>chgy1234</username><title></title><pdstate></pdstate><pageNumber>%d</pageNumber><pageSize>20</pageSize></body></message>",_currentPage];;

  bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bgetProductList];
  [HttpTools requestWithURL:path arrays:array method:@"getProductList" success:^(id JSON) {
//      [MBProgressHUD hideHUDForView:self.view animated:YES];
  NSLog(@"%@",JSON);
   [self.view makeToast:JSON[@"msg"]];
                  if([[NSString stringWithFormat:@"%@",JSON[@"code"]] isEqualToString:@"200"]){
                      
        _dataArray =   [NSMutableArray array];
NSDictionary *dic = JSON[@"ProductsList"];
NSArray *array =dic[@"list"];

for (NSDictionary *dic in array ) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
//_dataArray = dic[@"list"];
_lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
[_tableView reloadData];
        [self.tableView footerEndRefreshing];

        self.currentPage++;
                  }
    } failure:^(NSError *error) {
        [self.tableView footerEndRefreshing];
//      [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];

}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"getProductLis";

  getProductListTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[getProductListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
 NSDictionary *dic = self.dataArray[indexPath.row];
    cell.dic = dic;
    return cell;



 }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 
    return  120;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSDictionary *dic = _dataArray[indexPath.row];
     shangpinxqViewController *vc  = [[shangpinxqViewController alloc]init];
    
     vc.pdid= dic[@"id"];
     vc.hidesBottomBarWhenPushed = YES;
     [self.navigationController pushViewController:vc animated:YES];

}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSDictionary *dic = _dataArray[indexPath.row];
if ([[NSString stringWithFormat:@"%@",dic[@"state"]]isEqualToString:@"1"]) {

    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"删除", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了删除");
        NSDictionary *dic = _dataArray[indexPath.row];
        [self scaction:dic[@"id"]];
        }];
      deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"ff5a2d"];

    // 删除一个置顶按钮
    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"下架", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了下架");
            NSDictionary *dic = _dataArray[indexPath.row];
        [self xjaction:dic[@"id"]];
        }];
    topRowAction.backgroundColor = [UIColor colorWithHexString:@"ff8607"];
    
    // 添加一个更多按钮
    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"修改", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了修改");
          NSDictionary *dic = _dataArray[indexPath.row];
        [self xgaction:dic[@"id"]];
        
    }];
        moreRowAction.backgroundColor = [UIColor colorWithHexString:@"ffc208"];

//    moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    // 将设置好的按钮放到数组中返回
    return @[deleteRowAction, topRowAction];
    }else{

    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"删除", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了删除");
        NSDictionary *dic = _dataArray[indexPath.row];
        [self scaction:dic[@"id"]];
        }];
      deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"ff5a2d"];

    // 删除一个置顶按钮
    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"下架", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了下架");
            NSDictionary *dic = _dataArray[indexPath.row];
        [self xjaction:dic[@"id"]];
        }];
    topRowAction.backgroundColor = [UIColor colorWithHexString:@"ff8607"];
    
    // 添加一个更多按钮
    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"修改", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了修改");
          NSDictionary *dic = _dataArray[indexPath.row];
        [self xgaction:dic[@"id"]];
        
    }];
        moreRowAction.backgroundColor = [UIColor colorWithHexString:@"ffc208"];

//    moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    // 将设置好的按钮放到数组中返回
    return @[deleteRowAction, topRowAction, moreRowAction];
    }
}
- (void)xgaction:(NSString *)idstring{
addProductListViewController *vc = [[addProductListViewController alloc]init];
vc.ipid = idstring;
vc.type = @"xiuggai";
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}

- (void)scaction:(NSString *)idstring{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><id>%@</id></body></message>",userName,password,website,language,BAtype,idstring];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bgetProductList];
 [HttpTools requestWithURL:path arrays:array method:@"deleteProduct" success:^(id JSON) {
   NSLog(@"%@",JSON);

    [self.view makeToast:JSON[@"msg"]];
//[_tableView reloadData];
[self loadData];
 } failure:^(NSError *error) {

    }];

}

- (void)xjaction:(NSString *)idstring{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><ids>%@</ids></body></message>",userName,password,website,language,BAtype,idstring];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bgetProductList];
 [HttpTools requestWithURL:path arrays:array method:@"xjProduct" success:^(id JSON) {
   NSLog(@"%@",JSON);

    [self.view makeToast:JSON[@"msg"]];
[_tableView reloadData];
 } failure:^(NSError *error) {

    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
